from django.http import JsonResponse
from django.shortcuts import render
import psycopg2
from django.conf import settings

def hello_api(request):
    db_config = settings.DATABASES['default']
    conn = psycopg2.connect(
        host=db_config['HOST'],
        database=db_config['NAME'],
        user=db_config['USER'],
        password=db_config['PASSWORD'],
        port=db_config['PORT']
    )
    cur = conn.cursor()
    cur.execute("SELECT message FROM test_table LIMIT 1;")
    row = cur.fetchone()
    cur.close()
    conn.close()
    message = row[0] if row else "No data found"
    return JsonResponse({"message": message})

def index(request):
    return render(request, 'index.html')
